package com.workerhelping.controller;

import com.workerhelping.elasticsearchEntity.ESRecordVO;
import com.workerhelping.result.Result;
import com.workerhelping.result.ResultCodeEnum;
import com.workerhelping.service.ESRecordVOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应使用redis缓存保存失效es操作
 */
@RestController
@RequestMapping("/ESRecord")
public class ESRecordVOController {

    @Autowired
    private ESRecordVOService esRecordVOService;


    /**
     * 插入或保存档案信息
     *
     * @param esRecordVO
     * @return
     */
    @PostMapping("/insertOrUpdate")
    public Result<String> insertOrUpdateRecord(@RequestBody ESRecordVO esRecordVO) {
        try {
            esRecordVOService.insertOrUpdate(esRecordVO);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.INDEX_UPDATE_FAIL);
        }
    }

    /**
     * 删除档案信息
     *
     * @param esRecordVO
     * @return
     */
    @PostMapping("/delete")
    public Result<String> deleteRecord(@RequestBody ESRecordVO esRecordVO) {
        try {
            esRecordVOService.delete(esRecordVO);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.INDEX_DELETE_FAIL);
        }
    }

    /**
     * 未审核
     *
     * @param id
     * @param name
     * @param idNumber
     * @param creditCardNumbers
     * @return
     */
    @GetMapping("/findAllByUnreviewed")
    public Result<List<ESRecordVO>> findAllByUnreviewed(@RequestParam(value = "sId", required = false) Integer id,
                                                        @RequestParam(value = "sName", required = false) String name,
                                                        @RequestParam(value = "sIdNumber", required = false) String idNumber,
                                                        @RequestParam(value = "sCreditCardNumbers", required = false) String creditCardNumbers) {
        try {
            List<ESRecordVO> records = esRecordVOService.findAllByUnreviewed(id, name, idNumber, creditCardNumbers);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 已通过
     *
     * @param id
     * @param name
     * @param idNumber
     * @param creditCardNumbers
     * @return
     */
    @GetMapping("/findAllByApproved")
    public Result<List<ESRecordVO>> findAllByApproved(@RequestParam(value = "sId", required = false) Integer id,
                                                      @RequestParam(value = "sName", required = false) String name,
                                                      @RequestParam(value = "sIdNumber", required = false) String idNumber,
                                                      @RequestParam(value = "sCreditCardNumbers", required = false) String creditCardNumbers) {
        try {
            List<ESRecordVO> records = esRecordVOService.findAllByApproved(id, name, idNumber, creditCardNumbers);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 已拒绝
     *
     * @param id
     * @param name
     * @param idNumber
     * @param creditCardNumbers
     * @return
     */
    @GetMapping("/findAllByRejected")
    public Result<List<ESRecordVO>> findAllByRejected(@RequestParam(value = "sId", required = false) Integer id,
                                                        @RequestParam(value = "sName", required = false) String name,
                                                        @RequestParam(value = "sIdNumber", required = false) String idNumber,
                                                        @RequestParam(value = "sCreditCardNumbers", required = false) String creditCardNumbers) {
        try {
            List<ESRecordVO> records = esRecordVOService.findAllByRejected(id, name, idNumber, creditCardNumbers);
            return Result.ok(records);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 已注销
     *
     * @param id
     * @param name
     * @param idNumber
     * @param creditCardNumbers
     * @return
     */
    @GetMapping("/findAllByCancelled")
    public Result<List<ESRecordVO>> findAllByCancelled(@RequestParam(value = "sId", required = false) Integer id,
                                                        @RequestParam(value = "sName", required = false) String name,
                                                        @RequestParam(value = "sIdNumber", required = false) String idNumber,
                                                        @RequestParam(value = "sCreditCardNumbers", required = false) String creditCardNumbers) {
        try {
            List<ESRecordVO> records = esRecordVOService.findAllByCancelled(id, name, idNumber, creditCardNumbers);
            return Result.ok(records);
    } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
